//
//  PourOutDetailViewController.m
//  Toyohu
//
//  Created by celia on 16/3/31.
//  Copyright (c) 2016年 celia. All rights reserved.
//

#import "PourOutDetailViewController.h"
#import "Masonry.h"

#import "AnswerCell.h"
#import "cellForPourTableViewCell.h"

static NSString *ReuseId = @"AnswerCell";
static NSString *cellForPourReuseId = @"cellForPourTableViewCell";

#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height


@interface PourOutDetailViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *myTable;
    NSMutableArray *array;
}
@property (assign, nonatomic) CGFloat originCellMaxY;
@end

@implementation PourOutDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.originCellMaxY = [self getCellLastControlMaxY];
    UILabel *navLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
    navLabel.textColor = [UIColor whiteColor];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"倾诉";
    navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    self.navigationItem.titleView = navLabel;
    [self createCustomNavigationBackButton];
    
    myTable = [[UITableView alloc]initWithFrame:CGRectMake(0,64,WIDTH,HEIGHT-64-44) style:UITableViewStylePlain];
    myTable.delegate = self;
    myTable.dataSource = self;
    [myTable registerNib:[UINib nibWithNibName:@"AnswerCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseId];
    [myTable registerNib:[UINib nibWithNibName:@"cellForPourTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellForPourReuseId];
    
    myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:myTable];
    
    array= [@[
              @"asldfjasdjiofisoadfjioasojidfjioasoidfjioasdijofjoasdiojf",
              @"sadfasldfjasopdfkpasdfkasodfkpoasdfkpasdkfpasokdfpoasdfpaoskdpoaksgpfogjapodfjgdpsofjgpdsfgjdpfjgoasdkpoasdgkopasd"
              ] mutableCopy];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createBottomView];
    
    // Do any additional setup after loading the view.
}


-(void)createBottomView{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-44, WIDTH, 60)];
    myView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 60, 30)];
    label.text = @"写评论";
    label.textAlignment = NSTextAlignmentCenter;
    [myView addSubview:label];
    
    myView.layer.borderWidth = 1;
    myView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(3*WIDTH/5, 5, 60, 30);
    
    [leftButton setImage:[[UIImage imageNamed:@"收藏未选中@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    // 选中按钮以后 让按钮显示选中的那张背景图
    [leftButton setImage:[[UIImage imageNamed:@"收藏选中@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    [leftButton addTarget:self action:@selector(leftBtPress:) forControlEvents:UIControlEventTouchDown];
    
    [myView addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [rightButton setImage:[[UIImage imageNamed:@"帮顶未选中@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [rightButton setImage:[[UIImage imageNamed:@"帮顶选中@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    [rightButton addTarget:self action:@selector(rightBtPress:) forControlEvents:UIControlEventTouchDown];
    [myView addSubview:rightButton];
    
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(leftButton.mas_centerY);
        make.left.equalTo(leftButton.mas_right).offset(10);
        make.width.equalTo(@(60));
        make.height.equalTo(@(30));
        
    }];
    
    
    
    [self.view addSubview:myView];
}

-(void)leftBtPress:(UIButton *)sender{
    sender.selected = !sender.selected;
}

-(void)rightBtPress:(UIButton *)sender{
    sender.selected = !sender.selected;
}

-(void)createCustomNavigationBackButton{
    //导航栏左边项
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 10, 15);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"ARROW---LEFT@2x"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(goBackPreviousPage) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    

}

-(void)goBackPreviousPage{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        AnswerCell *cell = [myTable dequeueReusableCellWithIdentifier:ReuseId ];
        //取消系统自带的选中的cell的颜色
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        myTable.rowHeight = 238;
        if (!cell) {
            cell = (AnswerCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: ReuseId];
        }
        cell.content.text = @"对疫苗认知为0";
        cell.userImg.image = [UIImage imageNamed:@"图层-28@2x"];
        cell.userName.text = @"快乐的小蜜蜂";
        cell.title.text = @"小蜜蜂";
        cell.time.text = @"4小时前";
        cell.star.text  = @"1344";
        cell.counts.text = @"445";
        return cell;
    }
    else if (indexPath.row==1){
        cellForPourTableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:cellForPourReuseId ];
        //取消系统自带的选中的cell的颜色
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        myTable.rowHeight = 92;
        if (!cell) {
            cell = (cellForPourTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellForPourReuseId];
        }
        cell.img.image = [UIImage imageNamed:@"图层-28@2x"];
        
        cell.time.text = @"2016-03-24 13：57";
        cell.name.text = @"1234";
        cell.name.text = @"hahha";
        cell.content.text = array[0];
        // 获取不确定文字的高度
        NSString *str = array[0];
        CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:14.0] maxW:cell.frame.size.width withContent:str] + 10;
        
        // 重新设置cell的frame.
        CGRect frame = cell.frame;
        frame.size.height = testHeight + self.originCellMaxY;
        cell.frame = frame;
        return cell;
    }
    
    else{
        cellForPourTableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:cellForPourReuseId ];
        //取消系统自带的选中的cell的颜色
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        myTable.rowHeight = 92;
        if (!cell) {
            cell = (cellForPourTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellForPourReuseId];
        }
        cell.img.image = [UIImage imageNamed:@"图层-28@2x"];
        cell.time.text = @"2016-03-24 13：57";
        cell.name.text = @"1234";
        cell.name.text = @"hahha";
        cell.content.text = array[1];
        // 获取不确定文字的高度
        NSString *str = array[1];
        CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:14.0] maxW:cell.frame.size.width withContent:str] + 10;
        
        // 重新设置cell的frame.
        CGRect frame = cell.frame;
        frame.size.height = testHeight + self.originCellMaxY;
        cell.frame = frame;
        return cell;
    }
    
}

//每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 直接返回该indexPath对应cell的frame.size.height. (在cellForRowAtIndexPath方法中会重新更改)
    AnswerCell *cell = (AnswerCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}


// 计算指定文字的高度
-(CGFloat)sizeWithFont:(UIFont *)font maxW:(CGFloat) maxW withContent:(NSString *)testStr{
    
    NSDictionary *textAttrs = @{NSFontAttributeName : font};
    CGSize size = CGSizeMake(maxW, MAXFLOAT);
    return [testStr boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;
}

// 获取cell最底部控件的最大Y值
-(CGFloat)getCellLastControlMaxY {
    // 获取cell中最底部的控件
    cellForPourTableViewCell *tempCell = (cellForPourTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:@"cellForPourTableViewCell" owner:nil options:nil] lastObject];
    UIView *lastView = tempCell.lastView;
    return  lastView.frame.origin.y + lastView.bounds.size.height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
